package com.tchy.cloud.controller;

import com.tchy.cloud.entity.ACommentEntity;
import com.tchy.cloud.entity.AQuestionsEntity;
import org.jboss.logging.Logger;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.common.model.json.ValidForm;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MutiLangUtil;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.oConvertUtils;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * 问答管理
 * Created by zjy on 2015/9/22.
 */
@Controller
@RequestMapping("/questionsController")
public class AQuestionsController {

    private static final Logger logger = Logger.getLogger(AQuestionsController.class);

    @Autowired
    private SystemService systemService;

    private String message;


    /**
     * 问答管理列表页
     * @return
     */
    @RequestMapping(params = "questionsList")
    public ModelAndView questionsList(){
        return new ModelAndView("clound/articleManager/questions/questionsList");
    }


    /**
     * 问答管理列表
     * @param carEntity
     * @param request
     * @param response
     * @param dataGrid
     */
    @RequestMapping(params = "datagrid")
    public void datagrid(AQuestionsEntity carEntity, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid){

        CriteriaQuery cq = new CriteriaQuery(AQuestionsEntity.class,dataGrid);

        cq.add();
        // 查询条件组装
        org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, carEntity, request.getParameterMap());
        this.systemService.getDataGridReturn(cq,true);

//        //查询当前被占用车辆信息
//        String findCarStatusSQL = " select t2.car_id,t2.apply_status,t1.proposer,t3.realname from a_apply_car t2 \n" +
//                "left join a_apply t1 on t2.apply_id = t1.apply_id\n" +
//                "left join t_s_base_user t3 on t1.proposer = t3.id \n" +
//                "where t2.apply_status = 13 or t2.apply_status = 14  ";
//
//
//        List<Map<String,Object>> carStatusList = systemService.findForJdbc(findCarStatusSQL);
//        for(Object carObject : dataGrid.getResults()){
//
//            AQuestionsEntity carInfo = (AQuestionsEntity)carObject;
//
//            for(Map<String,Object> carStatus : carStatusList){
//
//                if(carInfo.getCarId() == Long.parseLong(String.valueOf(carStatus.get("car_id")))){
//
//                    carInfo.setUseUserName(String.valueOf(carStatus.get("realname")));//放入使用人姓名
//                    carInfo.setUseCarApplyStatus(String.valueOf(carStatus.get("apply_status")));
//                }
//
//            }
//
//        }
        TagUtil.datagrid(response, dataGrid);
    }


    /**
     * 跳转到修改转视图
     * @return
     */
    @RequestMapping(params = "addorupdate")
    public ModelAndView addorupdate(AQuestionsEntity questionsEntity,
                                    HttpServletRequest req) {


        if (questionsEntity.getQuestionsId() != null) {
            questionsEntity = systemService.getEntity(AQuestionsEntity.class, questionsEntity.getQuestionsId());

            req.setAttribute("questionsEntity", questionsEntity);

        }


        return new ModelAndView("clound/carManage/carInfo/bCar");
    }





    /**
     * 添加问答管理
     *
     * @param questionsEntity
     * @return
     */
    @RequestMapping(params = "save")
    @ResponseBody
    public AjaxJson save(AQuestionsEntity questionsEntity, HttpServletRequest request) {

        AjaxJson j = new AjaxJson();
        if (questionsEntity.getQuestionsId() != null) {

            try {

                AQuestionsEntity c = systemService.get(AQuestionsEntity.class, questionsEntity.getQuestionsId());


                MyBeanUtils.copyBeanNotNull2Bean(questionsEntity, c);

                systemService.saveOrUpdate(c);

                message = MutiLangUtil.paramUpdSuccess("问答管理["+questionsEntity.getTitle()+"]");

            } catch (Exception e) {
                e.printStackTrace();
                message = MutiLangUtil.paramUpdFail("问答管理["+questionsEntity.getTitle()+"]");
            }
            systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
        } else {

            try {

//                questionsEntity.setCreateTime(new Timestamp(System.currentTimeMillis()));
//                questionsEntity.setDeleted(0);
//                questionsEntity.setIsStart(1);
                systemService.save(questionsEntity);

                message = MutiLangUtil.paramAddSuccess("问答管理["+questionsEntity.getTitle()+"]");
            } catch (Exception e) {
                e.printStackTrace();

                message = "保存问答管理["+questionsEntity.getTitle()+"]失败";
            }

            systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
        }
        j.setMsg(message);
        return j;
    }



    /**
     * 删除问答管理（逻辑删除）
     * @param id
     * @return
     */
    @RequestMapping(params = "del")
    @ResponseBody
    public AjaxJson del(String id){
        AjaxJson j = new AjaxJson();
        try {

            AQuestionsEntity questionsEntity = systemService.get(AQuestionsEntity.class,Long.valueOf(id));

            if(questionsEntity != null){

                systemService.delete(questionsEntity);
                message = "删除问答["+questionsEntity.getTitle()+"]成功";
            }else{
                message = "问答信息不存在，请刷新重试！";
            }


        } catch (Exception e) {

            message = "删除问答管理失败";
        }

        systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);

        j.setMsg(message);
        return j;
    }

    /**
     * 问答管理列表页
     * @return
     */
    @RequestMapping(params = "commentList")
    public ModelAndView commentList(HttpServletRequest request,String quesId){

        request.setAttribute("quesId",quesId);
        return new ModelAndView("clound/articleManager/questions/comment/commentList");
    }


    /**
     * 问答管理列表
     * @param commentEntity
     * @param request
     * @param response
     * @param dataGrid
     */
    @RequestMapping(params = "commentDatagrid")
    public void commentDatagrid(ACommentEntity commentEntity, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid){

        CriteriaQuery cq = new CriteriaQuery(ACommentEntity.class,dataGrid);

        cq.eq("type",2);
        cq.add();
        // 查询条件组装
        org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, commentEntity, request.getParameterMap());
        this.systemService.getDataGridReturn(cq,true);

        TagUtil.datagrid(response, dataGrid);
    }

    /**
     * 回复详情列表页
     * @return
     */
    @RequestMapping(params = "parentCommentList")
    public ModelAndView parentCommentList(HttpServletRequest request,String commentId){

        request.setAttribute("commentId",commentId);
        return new ModelAndView("clound/articleManager/questions/comment/parentCommentList");
    }


    /**
     * 回复详情列表
     * @param commentEntity
     * @param request
     * @param response
     * @param dataGrid
     */
    @RequestMapping(params = "parentCommentDatagrid")
    public void parentCommentDatagrid(ACommentEntity commentEntity, Long commId, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid){

        CriteriaQuery cq = new CriteriaQuery(ACommentEntity.class,dataGrid);

//        cq.eq("type",2);
        cq.eq("parent",commId);
        cq.add();
        // 查询条件组装
        org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, commentEntity, request.getParameterMap());
        this.systemService.getDataGridReturn(cq,true);

        TagUtil.datagrid(response, dataGrid);
    }

    /**
     * 删除问答管理（逻辑删除）
     * @param id
     * @return
     */
    @RequestMapping(params = "parentCommentDel")
    @ResponseBody
    public AjaxJson parentCommentDel(String id){
        AjaxJson j = new AjaxJson();
        try {

            ACommentEntity commentEntity = systemService.get(ACommentEntity.class,Long.valueOf(id));

            if(commentEntity != null){

                systemService.delete(commentEntity);
                message = "删除回复成功";
            }else{
                message = "回复信息不存在，请刷新重试！";
            }


        } catch (Exception e) {

            message = "删除回复失败";
        }

        systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);

        j.setMsg(message);
        return j;
    }

}
